package com.lmuse.xmr.foodspotting.ui.userpage;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.Toast;

import com.andreabaccega.widget.FormEditText;
import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.model.User;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.UserPresenterImpl;

import java.util.List;

import cn.bmob.v3.exception.BmobException;

public class ForgetPwdActivity extends AppCompatActivity implements Contract.UserView{

    private FormEditText emailEt;
    private Button sendBtn;

    private InputMethodManager inputMethodManager; //处理隐藏输入法
    private Contract.UserPresenter userPresenter;
    private CountDownTimer downTimer;

    private boolean allValid = true; //输入合法性

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        userPresenter = new UserPresenterImpl(this);
        initView();
    }

    void initView() {
        if (downTimer != null) {
            downTimer.cancel();
            downTimer = null;
        }
        inputMethodManager =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        emailEt = (FormEditText) findViewById(R.id.et_reset_email);
        sendBtn = (Button) findViewById(R.id.btn_send_reset_email);
        sendBtn.setOnClickListener(new ButtonClickListener());



        downTimer = new CountDownTimer(29 * 1000 + 1050, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                sendBtn.setBackgroundResource(R.drawable.btn_style_unclickable_circle);
                sendBtn.setClickable(false);
                sendBtn.setText(((int) millisUntilFinished / 1000 - 1) + "s");
            }

            @Override
            public void onFinish() {
                sendBtn.setBackgroundResource(R.drawable.btn_style_green_turn_dark_circle);
                sendBtn.setText("发送邮件");
                sendBtn.setClickable(true);
            }
        };
    }

    class ButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_send_reset_email:
                    downTimer.start();
                    checkInputValid();
                    break;
            }
        }
    }


    /**
     * 输入合法性检查
     */
    void checkInputValid() {
        FormEditText[] allFields = { emailEt};

        for (FormEditText field: allFields) {
            allValid = field.testValidity() && allValid;
        }

        if (allValid) {
            userPresenter.resetPasswordByEmail(emailEt.getText().toString());
            inputMethodManager.hideSoftInputFromWindow(emailEt.getWindowToken(), 0);
        } else {
            // EditText are going to appear with an exclamation mark and an explicative message.
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        userPresenter.onDetach();
        if (downTimer != null) {
            downTimer.cancel();
        }
    }

    @Override
    public void onSignUpSuccess(User user) {

    }

    @Override
    public void onSignUpFailed(String msg) {

    }

    @Override
    public void onSignInSuccess(User user) {

    }

    @Override
    public void onSignInFailed(String msg) {

    }

    @Override
    public void onUserInfoCache(User user) {

    }

    @Override
    public void onUpdateUserSuccess() {

    }

    @Override
    public void onUpdateUserFailed(String msg) {

    }

    @Override
    public void onRequestEmailVerifyResult(BmobException e) {

    }

    @Override
    public void onResetPasswordByEmailResult(BmobException e) {
        if (e != null) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "发送成功！", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onQueryUserByIdSuccess(User user) {

    }

    @Override
    public void onQueryUserByIdFailed(String msg) {

    }

    @Override
    public void onQueryUserAmountSuccess(int size) {

    }

    @Override
    public void onQueryUserAmountFailed(String msg) {

    }

    @Override
    public void onQueryByKeySuccess(List<User> userList) {

    }

    @Override
    public void onQueryByKeyFailed(String msg) {

    }
}
